package com.version.manager.service;

import com.version.manager.entity.RoleUser;
import com.version.manager.entity.User;
import com.version.manager.mapper.ViewUserMapper;
import com.version.manager.model.Msg;
import com.version.manager.vo.ViewUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class ViewUserService extends BaseService<ViewUserMapper, ViewUser> {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleUserService roleUserService;
    public Msg addUser(ViewUser viewUser) throws Exception {
        User u=new User();
        u.setStatus("1");
        u.setName(viewUser.getName());
        u.setUserName(viewUser.getUserName());
        u.setCompanyId(Integer.parseInt(viewUser.getCompanyId()));
        u.setPassWord("123");
        u.setMobile(viewUser.getMobile());
        RoleUser roleUser=new RoleUser();


        Msg msg= userService.saveOrUpdate(u);
        User one=userService.selectOne(u);

        roleUser.setRoleId(Integer.parseInt(viewUser.getRoleId()));
        roleUser.setUserId(one.getId());
        msg=roleUserService.saveOrUpdate(roleUser);

        return msg;
    }

    public Msg editUser(ViewUser viewUser) throws Exception {
        User e=new User();
        e.setId(Integer.parseInt(viewUser.getId()));

        e=userService.selectOne(e);

        e.setName(viewUser.getName());
        e.setMobile(viewUser.getMobile());
        return userService.saveOrUpdate(e);

    }

    public Msg deleteUser(ViewUser viewUser) throws Exception {
        User e=new User();
        e.setId(Integer.parseInt(viewUser.getId()));

        e=userService.selectOne(e);
        e.setStatus("0");
        return userService.saveOrUpdate(e);

    }
}
